package com.zy.controller;

import com.zy.error.BusinessException;
import com.zy.response.CommonReturnType;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.HashMap;

/**
 * Created by zhaoyang on 2019/6/24.
 */

public class BaseController {
    @ExceptionHandler({Exception.class})
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    protected Object handleException(Exception e) {
        HashMap<String, String> map = new HashMap<>();
        if (e instanceof BusinessException) {
            BusinessException businessException = (BusinessException) e;
            map.put(businessException.getErrorCode(),businessException.getErrorMsg());
        }else {
            map.put(e.getMessage(),"");
        }

        return CommonReturnType.createFail(map);
    }

}
